package com.ptszyxx.popose.module.main.mine.vm;

import android.app.Application;

import androidx.annotation.NonNull;

import com.ptszyxx.popose.common.enums.MainTabEnum;
import com.ptszyxx.popose.common.utils.YActivityUtil;
import com.ysg.base.BaseViewModel;
import com.ysg.bus.event.SingleLiveEvent;
import com.ysg.http.BaseListResponse;
import com.ysg.http.BaseResponse;
import com.ysg.http.HttpUtils;
import com.ysg.http.callback.OnSuccessListResult;
import com.ysg.http.callback.OnSuccessResult;
import com.ysg.http.data.CommonRepository;
import com.ysg.http.data.entity.mine.SignAddResult;
import com.ysg.http.data.entity.mine.SignResult;
import com.ysg.http.data.entity.mine.TaskEntity;
import com.ysg.utils.YCollectionUtil;
import com.ysg.utils.YSPUtils;
import com.ysg.utils.YStringUtil;
import com.ysg.utils.YToastUtil;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class MineTaskVM extends BaseViewModel<CommonRepository> {

    public MineTaskVM(@NonNull Application application, CommonRepository model) {
        super(application, model);
    }

    public UIChangeObservable uc = new UIChangeObservable();
    public class UIChangeObservable {
        public SingleLiveEvent onRefreshNewEvent = new SingleLiveEvent<>();
        public SingleLiveEvent onRefreshDayEvent = new SingleLiveEvent<>();
        public SingleLiveEvent onSignEvent = new SingleLiveEvent<>();
    }

    //=========================================接口：start===========================================
    /**
     *
     */
    public void requestTaskList(){
        Map<String, String> map = new HashMap<>();
        HttpUtils.getInstance()
                .list(model.taskList(map), this,
                        new OnSuccessListResult<TaskEntity>() {
                            @Override
                            public void onSuccessResult(BaseListResponse<TaskEntity> response) {
                                List<TaskEntity> data = response.getData();
                                List<TaskEntity> newList = new ArrayList<>();
                                List<TaskEntity> dayList = new ArrayList<>();
                                if (YCollectionUtil.isNotEmpty(data)){
                                    for (TaskEntity item: data) {
                                        if (YStringUtil.eq(1, item.getType())){
                                            newList.add(item);
                                        } else if (YStringUtil.eq(2, item.getType())){
                                            dayList.add(item);
                                        }
                                    }
                                    uc.onRefreshNewEvent.setValue(newList);
                                    uc.onRefreshDayEvent.setValue(dayList);
                                }
                            }
                        });
    }

    public void onCompleteClick(TaskEntity entity){
        switch (entity.getTasknameEn()) {
            case "PERFECT_INFO":
                YActivityUtil.getInstance().jumpUserEdit(this, YSPUtils.getInstance().getUserId());
                break;
            case "REAL_NAME":
                YActivityUtil.getInstance().jumpMineReal(this);
                break;
            case "RECHARGE_FIRST":
                YActivityUtil.getInstance().jumpMineRecharge(this);
                break;
//            case "JOIN_FAMILY":
//                startActivity(new Intent(TaskCenterActivity.this, FamilyListActivity.class));
//                break;
            case "DYNAMIC_COMMEND"://动态评论
                YActivityUtil.getInstance().jumpMainActivity(this, MainTabEnum.dynamic);
                finish();
                break;
            case "GIFT_ONCE"://送礼
            case "FOCUS_USER"://关注
            case "HEART_BEAT":
                YActivityUtil.getInstance().jumpMainActivity(this, MainTabEnum.home);
                finish();
                break;
            case "MESSAGE_CHAT"://私信聊天
            case "VOICE_CHAT":
            case "VIDEO_CHAT":
                YActivityUtil.getInstance().jumpMainActivity(this, MainTabEnum.message);
                finish();
                break;
            case "VOICE_SIGNATURE"://语音签名
                YActivityUtil.getInstance().jumpUserEditVoice(this);
                break;
            case "SEND_DYNAMIC":
                YActivityUtil.getInstance().jumpDynamicRelease(this);
                break;
            case "SIGNON":
                requestSignList();
                break;

        }
    }

    /**
     * 签到记录
     */
    public void requestSignList(){
        Map<String, String> map = new HashMap<>();
        HttpUtils.getInstance()
                .data(model.signList(map), this, new OnSuccessResult<SignResult>() {
                    @Override
                    public void onSuccessResult(BaseResponse<SignResult> response) {
                        uc.onSignEvent.setValue(response.getData());
                    }
                });
    }

    /**
     * 签到
     */
    public void requestSignAdd(){
        Map<String, String> map = new HashMap<>();
        HttpUtils.getInstance()
                .data(model.signAdd(map), this, new OnSuccessResult<SignAddResult>() {
                    @Override
                    public void onSuccessResult(BaseResponse<SignAddResult> response) {
                        YToastUtil.showShort(response.getMsg());
                        requestTaskList();
                    }
                });
    }
    /**
     * 签到提醒
     */
    public void requestSignTip(){
        Map<String, String> map = new HashMap<>();
        HttpUtils.getInstance()
                .data(model.signTip(map), this, new OnSuccessResult() {
                    @Override
                    public void onSuccessResult(BaseResponse response) {
                    }
                });
    }

}
